package com.maven.front.controller.emailgli;


import com.maven.front.bean.EduEmailsendHistory;
import com.maven.front.bean.EduUser;
import com.maven.front.bean.Result;
import com.maven.front.bean.SysUser;
import com.maven.front.dto.EduEmailsendHistoryDto;
import com.maven.front.service.eduUser.EduUserService;
import com.maven.front.service.emailgli.EduEmailsendHistoryService;
import com.maven.front.service.sysuser.SysUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Slf4j
@CrossOrigin //解决跨域问题
@RestController
@RequestMapping("admin/edu")
public class EduEmailsendHistoryController {

    @Autowired
    private EduEmailsendHistoryService eduEmailsendHistoryService;
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private EduUserService eduUserService;
    @Resource
    JavaMailSender javaMailSender;

    //查询所有或者条件查询
    @GetMapping("listAll")
    public Result listAll(EduEmailsendHistory eduEmailsendHistory)throws Exception  {
        if (eduEmailsendHistory.getCreateTime()!=null){
            eduEmailsendHistory.setCreateTime(toDate(eduEmailsendHistory.getCreateTime()));
        }
        if (eduEmailsendHistory.getSendTime()!=null){
            eduEmailsendHistory.setSendTime(toDate(eduEmailsendHistory.getSendTime()));
        }

        List<EduEmailsendHistoryDto> list = eduEmailsendHistoryService.listAll(eduEmailsendHistory);
        System.out.println("list："+list.size());
        if (CollectionUtils.isEmpty(list)) {
            return new Result(1005, "查询失败", null);
        } else {

          return new Result(200,"查询成功",list);
        }
    }



    //查询
    @GetMapping("getid")
    public Result getId(int id){
        EduEmailsendHistory eduEmailsendHistory=eduEmailsendHistoryService.selectByPrimaryKey(id);
        //获取副表id
        int userid=eduEmailsendHistory.getUserId();
        //查询副表
        SysUser sysUser=sysUserService.selectByPrimaryKey(userid);
        //获取需要字段
        String name=sysUser.getLoginName();
        //继承表
        EduEmailsendHistoryDto eduEmailsendHistoryDto=new EduEmailsendHistoryDto();
        eduEmailsendHistoryDto.setName(name);
        //  将父类数据复制给子类数据
        BeanUtils.copyProperties(eduEmailsendHistory,eduEmailsendHistoryDto);
        if (eduEmailsendHistory==null) {
            return new Result(1005, "查询失败", null);
        } else {
            return new Result(200,"查询成功",eduEmailsendHistoryDto);
        }
    }

    //新增
    @PostMapping("save")
    public Result save(EduEmailsendHistory eduEmailsendHistory){
        eduEmailsendHistory.setUserId(1);
        int res=eduEmailsendHistoryService.insert(eduEmailsendHistory);
        if (res>0){
            return new Result(200,"新增成功");
        }else {
            return new Result(1009,"新增失败",null);
        }



    }

    @GetMapping("list")
    public Result list(){
        List<SysUser> list=sysUserService.list();
        if (CollectionUtils.isEmpty(list)) {
            return new Result(1007, "查询失败", null);
        } else {

            return new Result(200,"查询成功",list);
        }
    }

    @GetMapping("listedu")
    public Result list1(){
        List<EduUser> list=eduUserService.list();
        if (CollectionUtils.isEmpty(list)) {
            return new Result(1008, "查询失败", null);
        } else {
            return new Result(200,"查询成功",list);
        }
    }


    @GetMapping("getName")
    public Result getName(String id){
        return sysUserService.getid(id);
    }

    @GetMapping("getName1")
    public Result getName1(String id){
        return eduUserService.getid(id);
    }


    //    日期格式转换
    public Date toDate(Date date) throws Exception{
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String strdate = sdf.format(date);
        Date adddate = sdf.parse(strdate);
        return adddate;
    }

}
